package com.ljc.cjgl.controller;

import java.util.HashSet;
import java.util.List;

import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ljc.cjgl.custom.TablePageable;
import com.ljc.cjgl.custom.TableResult;
import com.ljc.cjgl.entitiy.Stu;
import com.ljc.cjgl.entitiy.TbClass;
import com.ljc.cjgl.entitiy.TbTest;
import com.ljc.cjgl.entitiy.TestRegister;
import com.ljc.cjgl.form.TestRegisterForm;
import com.ljc.cjgl.security.SysUser;
import com.ljc.cjgl.security.UserUtils;
import com.ljc.cjgl.service.ClassService;
import com.ljc.cjgl.service.StuService;
import com.ljc.cjgl.service.TestRegisterService;
import com.ljc.cjgl.service.TestService;

@Controller
@RequestMapping(value="/testRegister")
public class TestRegisterController {
	@Autowired
	private TestRegisterService service;
	@Autowired
	private TestService testService;
	@Autowired
	private StuService studentService;
	@Autowired
	private ClassService classService;
	@Autowired
	private UserUtils userUtils;
	
	@RequestMapping(value="/manage")
	public String manage(ModelMap map) {
		List<TbTest> tests = testService.findAll();
		List<TbClass> classes = classService.findAll();
		map.put("tests",tests);
		map.put("classes",classes);
		return "testRegister/manage";
	}
	
	private Specification<TestRegister> bulidSpec(TestRegisterForm form){
		HashSet<Rule> rules=new HashSet<>();
		rules.add(Rule.property("test.status").lessThan(2));
		Integer testId = form.getTestId();
		Integer classId = form.getClassId();
		String search=form.getSearch();
		SysUser user = userUtils.getUser();
		if(userUtils.hasRole("ROLE_STUDENT")) {
			rules.add(Rule.property("student.id").equal(user.getId()));
		}
		if(testId!=null) {
			rules.add(Rule.property("test.id").equal(testId));
		}
		if(classId!=null) {
			rules.add(Rule.property("student.tbClass.id").equal(classId));
		}
		if(StringUtils.hasText(search)) {
			HashSet<Rule> rules2=new HashSet<>();
			rules2.add(Rule.property("test.name").contain(search));
			rules2.add(Rule.property("student.name").contain(search));
			rules2.add(Rule.property("student.tbClass.name").contain(search));
			rules2.add(Rule.property("projectUrl").contain(search));
			rules.add(Rule.or(rules2));
		}
		return Rule.and(rules).toSpecification();
	}
	
	@RequestMapping("/page")
	@ResponseBody
	public Object page(TablePageable pageable,TestRegisterForm form) {
		PageRequest pageRequest = pageable.bulidPageRequest();
		Specification<TestRegister> spec=bulidSpec(form);
		Page<TestRegister> page=service.findAll(spec, pageRequest);
		return TableResult.bulidPageResult(page);
	}
	
	@RequestMapping("/edit")
	public String edit(Integer id,Integer testId,Integer classId,ModelMap map) {
		SysUser user=userUtils.getUser();
		TestRegister model=new TestRegister();
		if(id!=null) {
			model=service.findById(id);
			/*if(userUtils.hasRole("ROLE_STUDENT")) {
				Stu student=(Stu) user;
				model=service.findByStudentId(student.getId());
			}*/
		}
		map.put("model",model);
		List<TbTest> tests = testService.findByStatusLessThan(2);
		if(testId!=null) {
			tests.clear();
			tests.add(testService.findOne(testId));
		}
		map.put("tests",tests);
		Sort sort=Sort.by("tbClass.id","username");
		List<Stu> students=studentService.findAll(sort);
		if(classId!=null) {
			students=studentService.findByTbClassId(classId);
		}
		if(userUtils.hasRole("ROLE_STUDENT")) {
			students.clear();
			students.add((Stu) user);
		}
		map.put("students", students);
		return "testRegister/edit";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public Object save(TestRegisterForm form) {
		SysUser user=userUtils.getUser();
		if(userUtils.hasRole("ROLE_STUDENT")){
			Integer selfId=user.getId();
			if(form.getStudentId()!=selfId) {
				return TableResult.bulidResult(false, "不能修改别人的注册数据");
			}
		}
		service.save(form);
		return TableResult.bulidResult(true, "数据保存成功");
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Object delete(@RequestBody List<TestRegister> testRegisters) {
		if(service.delete(testRegisters)) {
			return TableResult.bulidResult(true, "数据删除成功");
		}else {
			return TableResult.bulidResult(false, "数据保存失败");
		}
	}
	
}
